var schedule = require('node-schedule');
let dbConfig = require('../utils/dbconfig');

let request = require('request');

function getBearerToken() {
    let params = {
        username: 'xsz',
        password: 'zzz123'
    }
    request({
        url: 'http://42.193.174.155:5700/api/login?t=1635676069852',
        method: 'POST',
        headers: {//设置请求头
            "content-type": "application/json",
        },
        body: JSON.stringify(params)//post参数字符串
    }, function (err, response, body) {
        console.log(body)
        let newBody = JSON.parse(body)
        setToken({ token: newBody.token, id: 1 })
    })
}
const setToken = async (data) => {
    const { token, id } = data
    let sql = `update qinglong set token =? where id = ? `
    let sqlArr = [token, id]
    let result = await dbConfig.SySqlConnect(sql, sqlArr)
    if (result && result.affectedRows) {
        console.log('success')
    }
}
function scheduleCronstyle() {
    schedule.scheduleJob('30 5 * * * *', function () {
        getBearerToken()
    });
}
module.exports = scheduleCronstyle